/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tlf.dyjc5.servlet.task;

import com.tlf.dyjc5.service.RsjService;
import com.tlf.dyjc5.service.TaskService;
import com.tlf.dyjc5.service.YsjService;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.dusin.web.util.JsonUtil;
import org.dusin.web.util.TheUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 *
 * @author tengyu
 */
public class TaskSave extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
            TaskService taskService = (TaskService) context.getBean("taskService");
            RsjService rsjService = (RsjService) context.getBean("rsjService");
            YsjService ysjService = (YsjService) context.getBean("ysjService");
            String msg = "补采任务已加入任务列表，请稍候查看数据...";
            String command = request.getParameter("cmd");
            boolean result = false;
            String currentOrgCode = (String) request.getSession().getAttribute("currentOrgCode");

            if ("createRTask".equals(command)) {
                String deviceIds = request.getParameter("deviceIds");
                String dates = request.getParameter("dates");
                result = taskService.addBatch(deviceIds, 10, dates);
            } else if ("createRAllTask".equals(command)) {
                String dateFrom = request.getParameter("dateFrom");
                String dateTo = request.getParameter("dateTo");

                String condition = TheUtil.getPointSelectCondition(request);
                List<Map<String, Object>> list = rsjService.findAllDataRecall(currentOrgCode, dateFrom, dateTo, condition, 0, 999999);

                StringBuilder deviceIds = new StringBuilder();
                StringBuilder dates = new StringBuilder();
                for (Map<String, Object> map : list) {
                    deviceIds.append(map.get("deviceId")).append(",");
                    dates.append(map.get("date")).append(",");
                }
                if (deviceIds.length() > 0) {
                    deviceIds.deleteCharAt(deviceIds.length() - 1);
                    dates.deleteCharAt(dates.length() - 1);
                }
                result = taskService.addBatch(deviceIds.toString(), 10, dates.toString());
            } else if ("createYTask".equals(command)) {
                String deviceIds = request.getParameter("deviceIds");
                String dates = request.getParameter("dates");
                result = taskService.addBatch(deviceIds, 20, dates);
            } else if("createYAllTask".equals(command)){                            
                String date = request.getParameter("dateTo");                
                String condition = TheUtil.getPointSelectCondition(request);              
                List<Map<String, Object>> list = ysjService.findAllDataRecall(currentOrgCode,  date, condition, 0, 9999);               
                StringBuilder deviceIds = new StringBuilder();
                StringBuilder dates = new StringBuilder();
                for (Map<String, Object> map : list) {
                    deviceIds.append(map.get("deviceId")).append(",");
                    dates.append(date).append(",");
                }
                if (deviceIds.length() > 0) {
                    deviceIds.deleteCharAt(deviceIds.length() - 1);
                    dates.deleteCharAt(dates.length() - 1);
                }
                result = taskService.addBatch(deviceIds.toString(), 20, dates.toString());
                
            } else if ("createRBackflowTask".equals(command)) {      //回灌日数据
                String deviceIds = request.getParameter("deviceIds");
                String dates = request.getParameter("dates");
                String lock = request.getParameter("lock");
                int type = 100;
                if("".equals(lock.trim()))
                    type  = 110;
                result = taskService.addBatch(deviceIds, type, dates);
            }else if ("createRBatchBackflowTask".equals(command)) {      //批量回灌日数据
                String dateFrom = request.getParameter("dateFrom");
                String dateTo = request.getParameter("dateTo");
                String lock = request.getParameter("lock");
                String condition = TheUtil.getPointSelectCondition(request);
                List<Map<String, Object>> list = rsjService.findAllDataRecall(currentOrgCode, dateFrom, dateTo, condition, 0, 999999);

                StringBuilder deviceIds = new StringBuilder();
                StringBuilder dates = new StringBuilder();
                for (Map<String, Object> map : list) {
                    deviceIds.append(map.get("deviceId")).append(",");
                    dates.append(map.get("date")).append(",");
                }
                if (deviceIds.length() > 0) {
                    deviceIds.deleteCharAt(deviceIds.length() - 1);
                    dates.deleteCharAt(dates.length() - 1);
                }
                int type = 100;
                if("".equals(lock.trim()))
                    type  = 110;
                result = taskService.addBatch(deviceIds.toString(), type, dates.toString());
            }else if ("createYBackflowTask".equals(command)) {      //回灌月数据
                String deviceIds = request.getParameter("deviceIds");
                String dates = request.getParameter("dates");
                String lock = request.getParameter("lock");
                int type = 200;
                if("".equals(lock.trim()))
                    type  = 210;
                result = taskService.addBatch(deviceIds, type, dates);
            }else if("createYBatchBackflowTask".equals(command)){ //批量回灌月数据
                String date = request.getParameter("dateTo");
                String lock = request.getParameter("lock");
                String condition = TheUtil.getPointSelectCondition(request);              
                List<Map<String, Object>> list = ysjService.findAllDataRecall(currentOrgCode,  date, condition, 0, 9999);               
                StringBuilder deviceIds = new StringBuilder();
                StringBuilder dates = new StringBuilder();
                for (Map<String, Object> map : list) {
                    deviceIds.append(map.get("deviceId")).append(",");
                    dates.append(date).append(",");
                }
                if (deviceIds.length() > 0) {
                    deviceIds.deleteCharAt(deviceIds.length() - 1);
                    dates.deleteCharAt(dates.length() - 1);
                }
                int type = 200;
                if("".equals(lock.trim()))
                    type  = 210;
                result = taskService.addBatch(deviceIds.toString(), type, dates.toString());
            }             
            out.println(JsonUtil.createSuccessJsonResponse(msg));

            /*if (result) {
             out.println(JsonUtil.createSuccessJsonResponse(msg));
             } else {
             out.print(JsonUtil.createFailureJsonResponse("数据库操作失败"));
             }*/

        }  catch (Exception e) {
            out.print(JsonUtil.createFailureJsonResponse("不明操作错误"));
            e.printStackTrace();
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
